package practica1;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

import weka.core.Instances;

/**
 * 1. LOAD DATA FILE
 * @author luciarodero
 */
public class LoadDataFile {
	//Attributes
	private String path;
	
	
	// 1.1. Get the path of the .arff (instances) from the command line
	public LoadDataFile(String pPath) {
		this.path = pPath;
	}

	public Instances loadInstances(){
				
		// 1.2. Open the file
		FileReader fi=null;
		try {
			fi= new FileReader(path);
		} catch (FileNotFoundException e) {
			System.out.println("ERROR: Revisar path del fichero de datos:"+path);
		}
		
		// 1.3. Load the instances
		Instances data=null;
		try {
			data = new Instances(fi);
		} catch (IOException e) {
			System.out.println("ERROR: Revisar contenido del fichero de datos: "+path);
		}
		
		// 1.4. Close the file
		try {
			fi.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// 1.5. Shuffle the instances: apply Randomize filter
		Random r = new Random(43);
		data.randomize(r);
		
		// 1.6. Specify which attribute will be used as the class: the last one, in this case 
		data.setClassIndex(data.numAttributes()-1);	
		
		return data;
	}
}
